Skip to content

SafeHeap: Prepare for emscripten_get_sbrk_ptr#2331

Merged
kripken merged 2 commits intomasterfrom
safes
Sep 6, 2019
Merged

SafeHeap: Prepare for emscripten_get_sbrk_ptr#2331
kripken merged 2 commits intomasterfrom
safes

Conversation

@kripken
Copy link
Copy Markdown
Member

@kripken kripken commented Sep 5, 2019

Currently emscripten links the wasm, then links the JS, then computes the final static allocations and in particular the location of the sbrk ptr (i.e. the location in memory of the brk location). Emscripten then imports that into the asm.js or wasm as env.DYNAMICTOP_PTR. However, this didn't work in the wasm backend where we didn't have support for importing globals from JS, so we implement sbrk in JS.

I am proposing that we change this model to allow us to write sbrk in C and compile it to wasm. To do so, that C code can import an extern C function, emscripten_get_sbrk_ptr(), which basically just returns that location. The PostEmscripten pass can even apply that value at compile time, so we avoid the function call, and end up in the optimal place, see #2325 and emscripten PRs will be opened once other stuff lands.

However, the SafeHeap pass must be updated to handle this, or our CI will break in the middle. This PR fixes that, basically making it check if env.DYNAMICTOP_PTR exists, or if not then looking for env.emscripten_get_sbrk_ptr, so that it can handle both.

Copy link
Copy Markdown
Member

@tlively tlively left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unfortunate that the test gets so huge, but LGTM.

@kripken kripken merged commit 7b4f978 into master Sep 6, 2019
@kripken kripken deleted the safes branch September 6, 2019 03:19
kripken added a commit that referenced this pull request Sep 6, 2019
To allow #2331 to roll, I forgot that upstream and fastcomp handle sbrk differently. This fixes that, and handles the upstream case where we import sbrk itself from JS.

We can simplify this after emscripten-core/emscripten#9397 lands, however, it may also be nice to keep the backwards compatibility for running on existing wasm files in the wild.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants